/**
 * This class will implement the login controller to our system. 
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 * 
 */
package control;

// Imports ********************************************

import java.io.Serializable;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import system.VcpClient;
import vcpClasses.ParkingLot;
import vcpClasses.User;

// Start of LoginController ***************************

public class LoginController extends AbstractController {
	
	//Class variables *********************************

	private SystemTypes newUserRequest;
	boolean flag = false;
	
	//Constructor *************************************
	
	/**
	 * Unimplemented constructor
	 */
	
	public LoginController(){};
	
	
	/**
	 * This method will receive all data from the GUI,
	 * will try to initiate connection to the server,
	 * if succeed, will send the request to the server and will return answer accordingly. 
	 * @param ip - user given IP for the server
	 * @param port - user given port for the server
	 * @param obj - this will be an instance of User that will be initialized at the GUI, according to user input
	 * @return - false if connection to server failed, or if verifying went wrong, and true otherwise.
	 */
	
	public SystemTypes verifyLoginDetails(String ip, int port, Serializable obj){
		try
		{
			VCP.client = new VcpClient(ip,port);
		}
		catch (Exception e)
		{
			newUserRequest = new SystemTypes(obj,SystemMode.CONN_ERR);
			return newUserRequest;
		}
		newUserRequest = new SystemTypes(obj,SystemMode.LOGIN);
		SendToServer(newUserRequest);
		while(!flag){}
		return newUserRequest;
	}
	
	/**
	 * This is the method that will send the GUI's input to the database through the server,
	 * using the client we initiated above. if the details received from the user verified against the DB,
	 * we'll save the client as the currentUser in the system.
	 * Moreover - if the connection is from kiosk then we'll save the lot as the currentLot
	 * @param obj - the User instance received from the GUI 
	 * @return true if validation succeeded, false otherwise 
	 */
	
	@Override
	public boolean SendToServer(Serializable obj) {
		
		VCP.client.send(obj);
		newUserRequest =(SystemTypes) VCP.client.getResult();
		Serializable arr[] = new Serializable[2];
		arr = (Serializable[]) this.newUserRequest.getObj();
		System.out.println(this.newUserRequest.getSysMode().toString());
		if (newUserRequest.getSysMode().equals(SystemMode.OK))
		{
			VCP.currentUser = (User) arr[0];
			if (((ParkingLot)arr[1]).getParkingLotNumber() != 0)
			{
				VCP.currentLot = new ParkingLot((ParkingLot)arr[1]);
			}
			newUserRequest.setObj(arr[0]);
			flag = true;
			return true;
		}
		newUserRequest.setObj(arr[0]);
		flag = true;
		return false;
	}
	
	/**
	 * this method will deal with all necessary actions when log out from the system
	 * @param sysType - the client to log out
	 * @return - the proper result 
	 */
	
	public static boolean LogOut(SystemTypes sysType){
		sysType.setSysMode(SystemMode.LOGOUT);
		VCP.client.send(sysType);
		sysType = (SystemTypes) VCP.client.getResult();
		if (sysType.getSysMode() == SystemMode.OK)
			return true;
		else
			return false;
	}
	

	
	// Getters and Setters ******************************
	/**
	 * @return the newUserReuqest
	 */
	public SystemTypes getNewUserRequest() {
		return newUserRequest;
	}

	/**
	 * @param newUserReuqest the newUserReuqest to set
	 */
	public void setNewUserRequest(SystemTypes t) {
		this.newUserRequest = t;
	}
	

}
